package cn.itcast.cart.controller;


import cn.itcast.cart.dto.ShoppingCartDTO;
import cn.itcast.cart.pojo.ShoppingCart;
import cn.itcast.cart.service.ShoppingCartService;
import cn.itcast.context.BaseContext;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 清空购物车
     *
     */
    @DeleteMapping(value = "/remote/clean/{userId}")
    public Result<String> remoteClean(@PathVariable("userId") Long userId){
        shoppingCartService.cleanShoppingCart(userId);
        return Result.success();
    }


    /**
     * 根据购物车信息查询购物车列表
     *
     */
    @PostMapping(value = "/search")
    public Result<List<ShoppingCart> > search(@RequestBody ShoppingCart shoppingCart){
        List<ShoppingCart> list = shoppingCartService.search(shoppingCart);

        return Result.success(list);
    }

    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @ApiOperation("添购物车")
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCartDTO dto){
        log.info("添加购物车：{}",dto);
        shoppingCartService.addShoppingCart(dto);

        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List> list(){
        log.info("查看购物车，当前用户:", BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCart = shoppingCartService.list();

        return Result.success(shoppingCart);
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param dto
     * @return
     */
    @ApiOperation("删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result<String> delete(@RequestBody ShoppingCartDTO dto){
        log.info("删除购物车中一个商品：{}",dto);
        shoppingCartService.delete(dto);

        return Result.success();
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> cleanShoppingCart(){
        log.info("清空购物车");
        shoppingCartService.cleanShoppingCart();

        return Result.success();
    }

}
